/*
  FileName : USB_Descriptor.h

  Desctiption :

  Created on 2007.9.19 by ZZP.

  Revision History:

    
*/


#ifndef __USB_DESCRIPTOR_H__
#define __USB_DESCRIPTOR_H__



///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
//                  Device controller define
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
//8-6-80 Device : 
#define MASS_STORAGE_CLASS    0x08
#define SCSI_SUBCLASS         0x06
#define BULK_ONLY_PROTOCOL    0x50

#define HUB_CLASS             0x09

#define BulkMaxPacketSize      0x40  //64 bytes for bulk/interrupt endpoint.
#define CtrlMaxPacketSize      0x20
//DC ID
#define VENDER_ID          0x0000
#define PRODUCT_ID         0x0000
#define DEVICE_RELEASE     0x0100

//string index
#define STR_INDEX_LANGUAGE				0x00	
#define STR_INDEX_MANUFACTURER			0x01	
#define STR_INDEX_PRODUCT				0x02	
#define STR_INDEX_SERIALNUMBER			0x03
#define STR_INDEX_CONFIGURATION			0x04	
#define STR_INDEX_INTERFACE				0x05

//Descriptor index
#define DEVICE_DESCRIPTOR               0x01
#define CONFIG_DESCRIPTOR               0x02
#define STRING_DESCRIPTOR               0x03
#define INTERFACE_DESCRIPTOR            0x04
#define ENDPOINT_DESCRIPTOR             0x05
#define DEVICE_QUALIFIER_DESCRIPTOR     0x06
#define OTHER_SPEED_CONFIG_DESCRIPTOR   0x07
#define INTERFACE_POWER_DESCRIPTOR      0x08

//Standard request index
#define GET_STATUS              0x00
#define CLEAR_FEATURE           0x01
#define SET_FEATURE             0x03
#define SET_ADDRESS             0x05
#define GET_DESCRIPTOR          0x06
#define SET_DESCRIPTOR          0x07
#define GET_CONFIG              0x08
#define SET_CONFIG              0x09
#define GET_INTERFACE           0x0A
#define SET_INTERFACE           0x0B
#define SYNCH_FRAME             0x0C


//Hub class request codes
#define HUB_GET_STATUS          0x00
#define HUB_CLEAR_FEATURE       0x01
#define HUB_RESERVED1           0x02
#define HUB_SET_FEATURE         0x03
#define HUB_RESERVED2           0x04
#define HUB_RESERVED3           0x05
#define HUB_GET_DESCRIPTOR      0x06
#define HUB_SET_DESCRIPTOR      0x07
#define HUB_CLEAR_TT_BUFFER     0x08
#define HUB_RESET_TT            0x09
#define HUB_GET_TT_STATE        0x0A
#define HUB_STOP_TT             0x0B

//Hub class feature selector
  //Recipient : Hub
#define C_HUB_LOCAL_POWER       0x00
#define C_HUB_OVER_CURRENT      0x01
  //Recipient : Port
#define PORT_CONNECTION         0x00
#define PORT_ENABLE             0x01
#define PORT_SUSPEND            0x02
#define PORT_OVER_CURRENT       0x03
#define PORT_RESET              0x04
#define PORT_POWER              0x08
#define PORT_LOW_SPEED          0x09
#define C_PORT_CONNECTION       0x10
#define C_PORT_ENABLE           0x11
#define C_PORT_SUSPEND          0x12
#define C_PORT_OVER_CURRENT     0x13
#define C_PORT_RESET            0x14
#define PORT_TEST               0x15
#define PORT_INDICATOR          0x16

//Mass storage, Bulk only:
#define MASS_BULK_RESET         0xFF
#define GET_MAX_LUN             0xFE


//Feature index
#define ENDPOINT_HALT           0x00
#define DEVICE_REMOTE_WAKEUP    0x01
#define TEST_MODE               0x02


//Endpoint
#define Number_EndPoint         0x02
#define MAX_ENDPOINTS           0x0F

#define ENDPOINT_TYPE_CONTROL      0x00
#define ENDPOINT_TYPE_ISOCHRONOUS  0x01
#define ENDPOINT_TYPE_BULK         0x02
#define ENDPOINT_TYPE_INTERRUPT    0x03

//EP1 : Bulk_Out   EP2 : Bulk_In
#define ENDPOINT_1_OUT          0x01
#define ENDPOINT_2_IN           0x82

#define ENDPSTS_HALT            0x01

#define USB_ENDPOINT_DIRECTION_MASK               0x80
#define USB_ENDPOINT_TYPE_MASK                    0x03

#define USB_FEATURE_ENDPOINT_STALL          0x0000
#define USB_FEATURE_REMOTE_WAKEUP           0x0001
#define USB_FEATURE_POWER_D0                0x0002
#define USB_FEATURE_POWER_D1                0x0003
#define USB_FEATURE_POWER_D2                0x0004
#define USB_FEATURE_POWER_D3                0x0005

// FSM for Default Control Pipe
// One-Hot dinfition4DCPFSM
#define USBFSM4DCP_IDLE             0x00
#define USBFSM4DCP_SETUPPROC        0x01
#define USBFSM4DCP_DATAIN           0x02
#define USBFSM4DCP_DATAOUT          0x04
#define USBFSM4DCP_HANDSHAKE4CO     0x08
#define USBFSM4DCP_HANDSHAKE4CI     0x10
#define USBFSM4DCP_STALL            0x80
#define USBFSM4DCP_BUSRESET         0x20

// FSM for Bulk-Only Transfer
// One-Hot dinfition4BOTFSM
#define USBFSM4BOT_STALL            0x80
#define USBFSM4BOT_IDLE             0x01
#define USBFSM4BOT_CBWPROC          0x02
#define USBFSM4BOT_DATAIN           0x04
#define USBFSM4BOT_DATAOUT          0x08
#define USBFSM4BOT_CSWPROC          0x10
#define USBFSM4BOT_CSW              0x20


#define USB_RECIPIENT            0x1F
#define USB_RECIPIENT_DEVICE     0x00
#define USB_RECIPIENT_INTERFACE  0x01
#define USB_RECIPIENT_ENDPOINT   0x02

#define USB_REQUEST_TYPE_MASK    0x60
#define USB_STANDARD_REQUEST     0x00
#define USB_CLASS_REQUEST        0x20
#define USB_VENDOR_REQUEST       0x40

#define USB_REQUEST_MASK         0xFF

#define DEVICE_ADDRESS_MASK      0x7F

#define DEVSTS_SELFPOWERED       0x01
#define DEVSTS_REMOTEWAKEUP      0x02


#define MAX_STD_REQUEST     12
#define MAX_CLASS_REQUEST   2
#define MAX_VENDOR_REQUEST  1


///////////////////////////////////////////////////////////
//	             Hub Descriptor													     
///////////////////////////////////////////////////////////
struct HUB_DESC_LEN_TYPE_BITS{
  Uns bDescLength : 8;
  Uns bDescType   : 8;
};
union HUB_DESC_LEN_TYPE_REG{
  Uns all;
  struct HUB_DESC_LEN_TYPE_BITS bit;
};

struct HUB_DESC_PORTNUM_CHARICLO_BITS{
  Uns bNbrPorts    : 8;
  Uns wHubCharicLo : 8;
};
union HUB_DESC_PORTNUM_CHARICLO_REG{
  Uns all;
  struct HUB_DESC_PORTNUM_CHARICLO_BITS bit;
};

struct HUB_DESC_CHARICHI_ON2GOODTM_BITS{
  Uns wHubCharicHi  : 8;
  Uns bPwrOn2GoodTm : 8;
};
union HUB_DESC_CHARICHI_ON2GOODTM_REG{
  Uns all;
  struct HUB_DESC_CHARICHI_ON2GOODTM_BITS bit;
};

struct HUB_DESC_CURRENT_REMOVABLE_BITS{
  Uns bHubContrCurrent : 8;
  Uns DeviceRemovalbe  : 8;
};
union HUB_DESC_CURRENT_REMOVABLE_REG{
  Uns all;
  struct HUB_DESC_CURRENT_REMOVABLE_BITS bit;
};

struct HUB_DESC_PWRMASK_BITS{
  Uns PortPwrCtrlMask : 8;
  Uns rsvd            : 8;
};
union HUB_DESC_PWRMASK_REG{
  Uns all;
  struct HUB_DESC_PWRMASK_BITS bit;
};

struct HUB_DESC_REG{
  union HUB_DESC_LEN_TYPE_REG Len_Type;
  union HUB_DESC_PORTNUM_CHARICLO_REG PortNbr_CharicLo;
  union HUB_DESC_CHARICHI_ON2GOODTM_REG CharicHi_On2GoodTm;
  union HUB_DESC_CURRENT_REMOVABLE_REG HubCurr_DevRemovable;
  union HUB_DESC_PWRMASK_REG PwrMask_Rsvd;
};





///////////////////////////////////////////////////////////
//	             Device Descriptor													     
///////////////////////////////////////////////////////////
struct DEV_DESC_D0_BITS{
  Uns bLength         : 8;
  Uns bDescriptorType : 8;
};
union DEV_DESC_D0_REG{
  Uns all;
  struct DEV_DESC_D0_BITS bit;
};

struct DEV_DESC_D1_BITS{
  Uns bcdUSBLo : 8;
  Uns bcdUSBHi : 8;
};
union DEV_DESC_D1_REG{
  Uns all;
  struct DEV_DESC_D1_BITS bit;
};

struct DEV_DESC_D2_BITS{
  Uns bDeviceClass    : 8;
  Uns bDeviceSubClass : 8;
};
union DEV_DESC_D2_REG{
  Uns all;
  struct DEV_DESC_D2_BITS bit;
};

struct DEV_DESC_D3_BITS{
  Uns bDeviceProtocol  : 8;
  Uns bMakPaketSize0   : 8;
};
union DEV_DESC_D3_REG{
  Uns all;
  struct DEV_DESC_D3_BITS bit;
};

struct DEV_DESC_D4_BITS{
  Uns idVendorLo  : 8;
  Uns idVerdorHi  : 8;
};
union DEV_DESC_D4_REG{
  Uns all;
  struct DEV_DESC_D4_BITS bit;
};

struct DEV_DESC_D5_BITS{
  Uns idProductLo  : 8;
  Uns idProductHi  : 8;
};
union DEV_DESC_D5_REG{
  Uns all;
  struct DEV_DESC_D5_BITS bit;
};

struct DEV_DESC_D6_BITS{
  Uns bcdDeviceLo  : 8;
  Uns bcdDeviceHi  : 8;
};
union DEV_DESC_D6_REG{
  Uns all;
  struct DEV_DESC_D6_BITS bit;
};

struct DEV_DESC_D7_BITS{
  Uns iManufacture  : 8;
  Uns iProduct      : 8;
};
union DEV_DESC_D7_REG{
  Uns all;
  struct DEV_DESC_D7_BITS bit;
};

struct DEV_DESC_D8_BITS{
  Uns iSerialNumber : 8;
  Uns bNumConfigs   : 8;
};
union DEV_DESC_D8_REG{
  Uns all;
  struct DEV_DESC_D8_BITS bit;
};

struct DEVICE_DESCRIPTOR_STRUCT{
  union DEV_DESC_D0_REG D0;
  union DEV_DESC_D1_REG D1;
  union DEV_DESC_D2_REG D2;
  union DEV_DESC_D3_REG D3;
  union DEV_DESC_D4_REG D4;
  union DEV_DESC_D5_REG D5;
  union DEV_DESC_D6_REG D6;
  union DEV_DESC_D7_REG D7;
  union DEV_DESC_D8_REG D8;
};

//extern volatile struct DEVICE_DESCRTPTOR_STRUCT Hc_XDeviceDesc;

/*typedef struct _DEVICE_DESCRIPTOR_STRUCT
{
  unsigned char bLength;
  unsigned char bDescriptorType;
  unsigned int  bcdUSB;
  unsigned char bDeviceClass;
  unsigned char bDeviceSubClass;
  unsigned char bDeviceProtocol;
  unsigned char bMaxPacketSize0;
  unsigned int  idVendor;
  unsigned int  idProduct;
  unsigned int  bcdDevice;
  unsigned char iManufacture;
  unsigned char iProduct;
  unsigned char iSerialNumber;
  unsigned char bNumConfigs;
}
DEVICE_DESCRIPTOR_STRUCT,*pDEVICE_DESCRIPTOR_STRUCT;*/

///////////////////////////////////////////////////////////
//	    High speed: Device qualifier descriptor													     
///////////////////////////////////////////////////////////
struct DEV_QLF_D0_BITS{
  Uns bLength   : 8;
  Uns bDescType : 8;
};
union DEV_QLF_D0_REG{
  Uns all;
  struct DEV_QLF_D0_BITS bit;
};

struct DEV_QLF_D1_BITS{
  Uns bcdUSBLo : 8;
  Uns bcdUSBHi : 8;
};
union DEV_QLF_D1_REG{
  Uns all;
  struct DEV_QLF_D1_BITS bit;
};

struct DEV_QLF_D2_BITS{
  Uns bClass    : 8;
  Uns bSubClass : 8;
};
union DEV_QLF_D2_REG{
  Uns all;
  struct DEV_QLF_D2_BITS bit;
};

struct DEV_QLF_D3_BITS{
  Uns bProtocol : 8;
  Uns bMaxPktSz : 8;
};
union DEV_QLF_D3_REG{
  Uns all;
  struct DEV_QLF_D3_BITS bit;
};

struct DEV_QLF_D4_BITS{
  Uns bNumConfigs : 8;
  Uns bRsvd       : 8;
};
union DEV_QLF_D4_REG{
  Uns all;
  struct DEV_QLF_D4_BITS bit;
};

struct DEVICE_QUALIFIER_STRUCT{
  union DEV_QLF_D0_REG D0;
  union DEV_QLF_D1_REG D1;
  union DEV_QLF_D2_REG D2;
  union DEV_QLF_D3_REG D3;
  union DEV_QLF_D4_REG D4;
};



///////////////////////////////////////////////////////////
//	             Configuration Descriptor													     
///////////////////////////////////////////////////////////
struct CFG_DESC_D0_BITS{
  Uns bLength         : 8;
  Uns bDescType       : 8;
};
union CFG_DESC_D0_REG{
  Uns all;
  struct CFG_DESC_D0_BITS bit;
};

struct CFG_DESC_D1_BITS{
  Uns wTotalLengthLo : 8;
  Uns wTotalLengthHi : 8;
};
union CFG_DESC_D1_REG{
  Uns all;
  struct CFG_DESC_D1_BITS bit;
};

struct CFG_DESC_D2_BITS{
  Uns bNumInterface  : 8;
  Uns bConfigValue   : 8;
};
union CFG_DESC_D2_REG{
  Uns all;
  struct CFG_DESC_D2_BITS bit;
};

struct CFG_DESC_D3_BITS{
  Uns iConfig      : 8;
  Uns bmAttributes : 8;
};
union CFG_DESC_D3_REG{
  Uns all;
  struct CFG_DESC_D3_BITS bit;
};

struct CFG_DESC_D4_BITS{
  Uns MaxPower     : 8;
  Uns rsvd         : 8;
};
union CFG_DESC_D4_REG{
  Uns all;
  struct CFG_DESC_D4_BITS bit;
};

struct CONFIG_DESCRIPTOR_STRUCT{
  union CFG_DESC_D0_REG D0;
  union CFG_DESC_D1_REG D1;
  union CFG_DESC_D2_REG D2;
  union CFG_DESC_D3_REG D3;
  union CFG_DESC_D4_REG D4;
};

//extern volatile struct CONFIG_DESCRIPTOR_STRUCT Hc_XCfgDesc;
/*	
typedef struct _COFIGURATION_DESCRIPTOR_STRUCT
{
  unsigned char bLength;
  unsigned char bDescriptorType;
  unsigned int  wTotalLength;
  unsigned char bNumInterfaces;
  unsigned char bConfigValue;
  unsigned char iConfig;
  unsigned char bmAttributes;
  unsigned char MaxPower;
}
CONFIG_DESCRIPTOR_STRUCT,*pCONFIG_DESCRIPTOR_STRUCT;   */

///////////////////////////////////////////////////////////
//	             Interface Descriptor													     
/////////////////////////////////////////////////////////// 
struct INTF_DESC_D0_BITS{
  Uns bLength       : 8;
  Uns bDescType     : 8;
};
union INTF_DESC_D0_REG{
  Uns all;
  struct INTF_DESC_D0_BITS bit;
};

struct INTF_DESC_D1_BITS{
  Uns bIntfNumber  : 8;
  Uns bAltSeting   : 8;
};
union INTF_DESC_D1_REG{
  Uns all;
  struct INTF_DESC_D1_BITS bit;
};

struct INTF_DESC_D2_BITS{
  Uns bNumEP     : 8;
  Uns bClass     : 8;
};
union INTF_DESC_D2_REG{
  Uns all;
  struct INTF_DESC_D2_BITS bit;
};

struct INTF_DESC_D3_BITS{
  Uns bSubClass   : 8;
  Uns bProtocol   : 8;
};
union INTF_DESC_D3_REG{
  Uns all;
  struct INTF_DESC_D3_BITS bit;
};

struct INTF_DESC_D4_BITS{
  Uns iInterface  : 8;
  Uns rsvd        : 8;
};
union INTF_DESC_D4_REG{
  Uns all;
  struct INTF_DESC_D4_BITS bit;
};

struct INTERFACE_DESCRIPTOR_STRUCT{
  union INTF_DESC_D0_REG D0;
  union INTF_DESC_D1_REG D1;
  union INTF_DESC_D2_REG D2;
  union INTF_DESC_D3_REG D3;
  union INTF_DESC_D4_REG D4;
};

//extern volatile struct INTERFACE_DESCRIPTOR_STRUCT Hc_XIntfDesc;
/*
typedef struct _INTERFACE_DESCRIPTOR_STRUCT
{
  unsigned char bLength;
  unsigned char bDescriptorType;
  unsigned char bInterfaceNumber;
  unsigned char bAlterlateSetting;
  unsigned char bNumEndPoints;
  unsigned char bInterfaceClass;
  unsigned char bInterfaceSubClass;
  unsigned char bInterfaceProtocol;
  unsigned char iInterface;
}
INTERFACE_DESCRIPTOR_STRUCT,*pINTERFACE_DESCRIPTOR_STRUCT; */

///////////////////////////////////////////////////////////
//	             EndPoint Descriptor													     
///////////////////////////////////////////////////////////
struct EP_DESC_D0_BITS{
  Uns bLength     : 8;
  Uns bDescType   : 8;
};
union EP_DESC_D0_REG{
  Uns all;
  struct EP_DESC_D0_BITS bit;
};

struct EP_DESC_D1_BITS{
  Uns bEPAddress    : 8;
  Uns bmAttributes  : 8;
};
union EP_DESC_D1_REG{
  Uns all;
  struct EP_DESC_D1_BITS bit;
};

struct EP_DESC_D2_BITS{
  Uns wMaxPktSizeLo  : 8;
  Uns wMaxPktSizeHi  : 8;
};
union EP_DESC_D2_REG{
  Uns all;
  struct EP_DESC_D2_BITS bit;
};

struct EP_DESC_D3_BITS{
  Uns bInterval  : 8;
  Uns rsvd       : 8;
};
union EP_DESC_D3_REG{
  Uns all;
  struct EP_DESC_D3_BITS bit;
};

struct ENDPOINT_DESCRIPTOR_STRUCT{
  union EP_DESC_D0_REG D0;
  union EP_DESC_D1_REG D1;
  union EP_DESC_D2_REG D2;
  union EP_DESC_D3_REG D3;
};
/*
typedef struct _ENDPOINT_DESCRIPTOR_STRUCT
{
  unsigned char bLength;
  unsigned char bDescriptorType;
  unsigned char bEndPointAddress;
  unsigned char bmAttributes;
  unsigned int  wMaxPacketSize;
  unsigned char bInterval;
}
ENDPOINT_DESCRIPTOR_STRUCT,*pENDPOINT_DESCRIPTOR_STRUCT;  */



typedef struct _USB_STRING_LANGUAGE_DESCRIPTOR {
	unsigned int DescType_Length;  //bLength;
	                               //INT8  bDescriptorType;
	unsigned int ulanguageID;
} USB_STRING_LANGUAGE_DESCRIPTOR,*PUSB_STRING_LANGUAGE_DESCRIPTOR;

typedef struct _USB_STRING_INTERFACE_DESCRIPTOR {
	unsigned int DescType_Length;  //bLength;
	                               //INT8  bDescriptorType;
	unsigned int Interface[11];
} USB_STRING_INTERFACE_DESCRIPTOR,*PUSB_STRING_INTERFACE_DESCRIPTOR;

typedef struct _USB_STRING_CONFIGURATION_DESCRIPTOR {
	unsigned int DescType_Length;  //bLength;
	                               //INT8  bDescriptorType;
	unsigned int Configuration[8];
} USB_STRING_CONFIGURATION_DESCRIPTOR,*PUSB_STRING_CONFIGURATION_DESCRIPTOR;

typedef struct _USB_STRING_SERIALNUMBER_DESCRIPTOR {
	unsigned int DescType_Length;  //bLength;
	                               //INT8  bDescriptorType;
	unsigned int SerialNum[12];
} USB_STRING_SERIALNUMBER_DESCRIPTOR,*PUSB_STRING_SERIALNUMBER_DESCRIPTOR;

typedef struct _USB_STRING_PRODUCT_DESCRIPTOR {
	unsigned int DescType_Length;  //bLength;
	                               //INT8  bDescriptorType;
	unsigned int Product[15];
} USB_STRING_PRODUCT_DESCRIPTOR,*PUSB_STRING_PRODUCT_DESCRIPTOR;

typedef struct _USB_STRING_MANUFACTURER_DESCRIPTOR {
	unsigned int DescType_Length;  //bLength;
	                               //INT8  bDescriptorType;
	unsigned int Manufacturer[12];
} USB_STRING_MANUFACTURER_DESCRIPTOR,*PUSB_STRING_MANUFACTURER_DESCRIPTOR;



typedef struct _STRING_DESCRIPTOR_STRUCT
{
  unsigned char bLength;
  unsigned char bDescriptorType;
  unsigned char SomeDescriptor[36];
}
STRING_DESCRIPTOR_STRUCT,*pSTRING_DESCRIPTOR_STRUCT;



typedef struct _REQUEST_CMD_STRUCT
{
  unsigned int bmReqTypeReq;
  unsigned int wValue;
  unsigned int wIndex;
  unsigned int wLength;
}
REQUEST_CMD_STRUCT,*pREQUEST_CMD_STRUCT;





#pragma CODE_SECTION(Dc_Interrupt_Isr, "FuncsMap2H0SARam");
extern void Dc_Interrupt_Isr(void);

extern void DcHandler(void);


#endif

//--------- End of File --------------



